#获取xml文件各类别的数目
import os
import random
import shutil

input_dir = "/home/xys/CloundShiProjects/traffic_light/trafficlight_dect/data/Annotations/"
jpg_dir = '/home/xys/CloundShiProjects/traffic_light/trafficlight_dect/data/JPEGImages/'
import xml.etree.ElementTree as ET
import xml.dom.minidom
num = 0
null = 0
classnames = {
    'traffic-light': 0

# 'hong-zuo':0,
# 'hong-zhi':0,
# 'lv-zuo':0,
# 'feijidongchedeng':0,
# 'hei':0,
# 'lv-zhi':0,
# 'huang-zuo':0,
# 'huang-zhi':0,
# 'hong-you':0,
# 'lv-you':0,
# 'huang-you':0
# 'bicycle':0, 'bus':0, 'car':0, 'motorbike':0, 'train':0, 'truck':0
# '__background__':0, 'aeroplane':0, 'bicycle':0, 'bird':0, 'boat':0,
#     'bottle':0, 'bus':0, 'car':0, 'cat':0, 'chair':0,
#     'cow':0, 'diningtable':0, 'dog':0, 'horse':0,
#     'motorbike':0, 'person':0, 'pottedplant':0,
#     'sheep':0, 'sofa':0, 'train':0, 'tvmonitor':0
    # 'vehicle':0,
    # 'e_vehicleHead':0,
    # 'e_vehicleTail':0,
    # 'e_licensePlate':0,
    # 'e_vehicleType':0,
    # 'e_frontMajorLamp':0,
    # 'e_frontMinorLamp':0,
    # 'e_backLamp':0,
    # 'e_vehiclePaddle':0,
    # 'e_luggageCarrier':0,
    # 'e_hub':0,
    # 'e_fanghuzhuangzhi':0,
    # 'e_weiyi':0,
    # 'e_exhaustHole':0,
    # 'e_vehicleBody':0,
    # 'e_rearViewMirror':0,
    # 'e_vehicleTripod':0,
    # 'e_reflectiveVest':0,
    # 'e_fuzhuzhidong':0,
    # 'e_abs':0,
    # 'e_asr':0,

    # 'person': 0,
    # 'vehicle': 0,
    # 'anquandai': 0,
    # 'xcjly': 0,
    # 'motorback':0,
    # 'emergency_hammer':0,
    # 'fire_extinguisher':0,
    # 'fire_extinguisher_meter':0,
    # 'IDcardZ':0,
    # 'IDcardF':0,
    # 'chejiahao':0,
    # 'mingpai':0,
    # 'xingshizheng_zhuye':0,
    # 'xingshizheng_fuye':0,
    # 'luntaiguige':0,
}
# for key in classnames.keys():
#     classnames[key] += 1
# print(classnames)
for filename in os.listdir(input_dir):
    file_path = os.path.join(input_dir, filename)
    dom = ET.parse(file_path)
    root = dom.getroot()

    # if root.find('object') is None:
    #     null+=1
    filename_front = filename[:-4]
    # if root.find('object') is None and random.randint(0,100)>99:
    if random.randint(0,100)>50:
        null+=1
        os.remove(input_dir+filename)
        os.remove(jpg_dir+filename_front+'.jpg')

    for obj in root.iter('object'):  # 获取object节点中的name子节点
        tmp_name = obj.find('name').text
        num = num +1
        if tmp_name in classnames.keys():
            classnames[tmp_name] += 1
print(num)
print(null)
print(classnames)

